package cappuccino.couldbook.ui.Adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cappuccino.couldbook.App;
import cappuccino.couldbook.R;
import cappuccino.couldbook.core.image.ImageReader;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.data.Group;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import cappuccino.couldbook.ui.QueryNumberActivity;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/12.
 */
public class RecylerApplicantsAdapter extends RecyclerView.Adapter<RecylerApplicantsAdapter.InfoViewHolder> {

    private static final String TAG = "RecylerGroupAdapter";
    List<Group> groups;
    Context context;

    public RecylerApplicantsAdapter(Context context, List<Group> groups) {
        this.groups = groups;
        this.context = context;
    }

    @Override
    public InfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        InfoViewHolder holder;
        switch (viewType) {
            case 0:
                holder = new GroupViewHolder(LayoutInflater.from(App.getInstance().getApplicationContext()).inflate(R.layout.item_group, parent, false));
                break;
            default:
                holder = new UserViewHolder(LayoutInflater.from(App.getInstance().getApplicationContext()).inflate(R.layout.item_info, parent, false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(InfoViewHolder holder, int position) {
        holder.setInfo(position);
    }

    @Override
    public int getItemViewType(int position) {
        Log.v(TAG, "pos:" + position);
        for (int i = 0; i < groups.size(); i++) {
            int size = groups.get(i).users.size();
            if (position > size) {
                position = position - size - 1;
            } else {
                if (position == 0) {
                    Log.v(TAG, "type:" + 0);
                    return 0;
                } else {
                    Log.v(TAG, "type:" + 1);
                    return 1;
                }
            }
        }
        return 1;
    }

    @Override
    public int getItemCount() {
        int size = 0;
        for (Group group : groups) {
            size = size + 1 + group.users.size();
        }
        return size;
    }

    abstract class InfoViewHolder extends RecyclerView.ViewHolder {
        public InfoViewHolder(View itemView) {
            super(itemView);
        }

        abstract public void setInfo(int position);
    }

    class UserViewHolder extends InfoViewHolder {
        TextView tv_user;
        ImageView iv_user;
        View view;

        public UserViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            tv_user = (TextView) itemView.findViewById(R.id.tv_bar_text);
            iv_user = (ImageView) itemView.findViewById(R.id.iv_bar_photo);

        }

        @Override
        public void setInfo(int position) {
            for (int i = 0; i < groups.size(); i++) {
                int size = groups.get(i).users.size();
                if (position > size) {
                    position = position - size - 1;
                } else {
                    tv_user.setText(groups.get(i).users.get(position - 1).userName);
                    setListener(view,groups.get(i).id,groups.get(i).users.get(position - 1).id);
                }
            }
        }

    }

    class GroupViewHolder extends InfoViewHolder {
        TextView tv_name;
        View view;

        public GroupViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            tv_name = (TextView) itemView.findViewById(R.id.tv_item_group);
        }

        @Override
        public void setInfo(int position) {
            for (int i = 0; i < groups.size(); i++) {
                int size = groups.get(i).users.size();
                if (position > size) {
                    position = position - size - 1;
                } else {
                    tv_name.setText(groups.get(i).name);
                }
            }
        }

    }

    private void setListener(View view, final int gid, final int uid) {
        Log.v(TAG,"gid:"+gid + "uid"+uid);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(context).setMessage("是否同意该申请").setNegativeButton("否", null).setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        NetApi api = Network.getNetApi();
                        api.passApplicant(uid, gid).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Result<Group>>() {
                            @Override
                            public void call(Result<Group> groupResult) {
                                if (groupResult.status == 1)
                                    Toast.makeText(context, "成功", Toast.LENGTH_SHORT).show();
                                else
                                    Toast.makeText(context, "失败", Toast.LENGTH_SHORT).show();
                            }
                        }, new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                Toast.makeText(context, "失败，请检查网络", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }).show();
            }
        });
    }
}
